package coolapk.cj.cj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import coolapk.cj.cj.dto.PersonDto;
import coolapk.cj.cj.entity.Goods;
import coolapk.cj.cj.entity.GroupMember;
import coolapk.cj.cj.entity.Person;
import coolapk.cj.cj.mapper.GroupMemberMapper;
import coolapk.cj.cj.mapper.PersonMapper;
import coolapk.cj.cj.service.GoodsService;
import coolapk.cj.cj.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wenjunjun9
 * @created 2021-02-16 18:51
 * @description
 */
@Slf4j
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {

    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GroupMemberMapper groupMemberMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addRecord(PersonDto personDto) {

        List<Goods> goodsList = goodsService.listByIds(personDto.getGoodsId());
        // 如果商品为空、已经开奖、已经有中奖的人
        if (CollectionUtils.isEmpty(goodsList)) {
            log.error("参与抽奖的商品不存在或者已过期, goodsId = {}", personDto.getGoodsId());
            throw new IllegalArgumentException("参与抽奖的商品不存在或者已过期！");
        }


        LambdaQueryWrapper<GroupMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupMember::getQq, personDto.getQq());
        GroupMember groupMember = groupMemberMapper.selectOne(queryWrapper);
        if (groupMember == null) {
            log.error("您不是群成员，无法抽奖！");
            throw new IllegalArgumentException("您不是群成员，无法抽奖！");
        }

        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Wrapper<Person> wrapper = Wrappers.<Person>lambdaQuery()
                .apply("date_format(create_time,'%Y-%m-%d') = {0}", formatter.format(currentDate))
                .eq(Person::getQq, personDto.getQq());
        List<Person> exist = list(wrapper);
        if (CollectionUtils.isNotEmpty(exist)) {
            throw new IllegalArgumentException("你以参与了今天的抽奖！");
        }

        List<Person> personList = goodsList.stream().map(goods -> {
            Person person = new Person();
            BeanUtils.copyProperties(personDto, person);
            person.setCreateTime(LocalDateTime.now());
            person.setGoodsId(goods.getId());
            person.setGoodsName(goods.getName());
            return person;
        }).collect(Collectors.toList());
        saveBatch(personList);
    }

    @Override
    public IPage<Person> pageQuery(Page<Person> page) {
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Wrapper<Person> wrapper = Wrappers.<Person>lambdaQuery()
                .apply("date_format(create_time,'%Y-%m-%d') = {0}", formatter.format(currentDate));
        return page(page, wrapper);
    }
}
